package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.MyorderMapper;
import com.chixing.mapper.StoreMapper;
import com.chixing.pojo.*;
import com.chixing.service.OrderService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/10/18:21
 * @Description:
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private MyorderMapper orderMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CarMapper carMapper;
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public ServerResponse save(Myorder order) {
        int rows = orderMapper.insert(order);
        if (rows > 0)
            return ServerResponse.getSucccess(rows);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getById(Integer orderId) {
        Myorder order = orderMapper.selectByPrimaryKey(orderId);
        if (order != null)
            return ServerResponse.getSucccess(order);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByOrderCustomer(Integer custId, Integer status) {
        List<OrderCarVO> orderCarVos = new ArrayList<>();
        MyorderExample orderExample = new MyorderExample();
        if (status == null) {
            orderExample.createCriteria().andCustIdEqualTo(custId);
        } else {
            orderExample.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(status);

        }
        List<Myorder> orders = orderMapper.selectByExample(orderExample);
        for (Myorder order : orders) {
            Car car = carMapper.selectByPrimaryKey(order.getCarId());
            orderCarVos.add(new OrderCarVO(order, car));
        }
        if (orderCarVos.size() != 0)
            return ServerResponse.getSucccess(orderCarVos);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByOrderId(Integer myOrderId) {
        Myorder order = orderMapper.selectByPrimaryKey(myOrderId);
        Integer custId = order.getCustId();
        Customer customer = customerMapper.selectByPrimaryKey(custId);
        Integer carId = order.getCarId();
        Car car = carMapper.selectByPrimaryKey(carId);
        Integer storeBorrowId = order.getRentstoreId();
        Store borrowStore = storeMapper.selectByPrimaryKey(storeBorrowId);
        Integer storeReturnId = order.getGivestoreId();
        Store returnStore = storeMapper.selectByPrimaryKey(storeReturnId);
        OrderCustomerStoreCarVO orderCustomerCarStoreVO = new OrderCustomerStoreCarVO(order, customer, car, borrowStore, returnStore);
        if (orderCustomerCarStoreVO != null)
            return ServerResponse.getSucccess(orderCustomerCarStoreVO);
        return ServerResponse.getFail(null);
    }

    @Override
    public Myorder getByOrderNum(Long orderNum) {
        MyorderExample orderExample = new MyorderExample();
        MyorderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andOrderNumEqualTo(orderNum);
        Myorder order = orderMapper.selectByExample(orderExample).get(0);
        return order;
    }

    @Override
    public ServerResponse getOrderCarAndStore(Integer orderId) {
        MyorderExample myorderExample = new MyorderExample();
        MyorderExample.Criteria criteria = myorderExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        Myorder myorder = orderMapper.selectByExample(myorderExample).get(0);
        Car car = carMapper.selectByPrimaryKey(myorder.getCarId());
        Store store = storeMapper.selectByPrimaryKey(myorder.getGivestoreId());
        OrderCarStoreVO orderCarStoreVO = new OrderCarStoreVO(myorder, store, car);
        if (orderCarStoreVO != null) {
            return ServerResponse.getSucccess(orderCarStoreVO);
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse saveOrder(Myorder order) {
        int rows = orderMapper.insert(order);
        if (rows > 0)
            return ServerResponse.getSucccess(rows);
        return ServerResponse.getFail(null);
    }

    //修改订单状态
    @Override
    public boolean updateMyOrderStatus(Integer orderId, Integer status) {
        Myorder myorder = orderMapper.selectByPrimaryKey(orderId);
        myorder.setStatus(status);
        return orderMapper.updateByPrimaryKey(myorder) > 0;
    }

    @Override
    public Myorder getMyorderId(Integer myOrderId) {
        return orderMapper.selectByPrimaryKey(myOrderId);
    }

    @Override
    public OrderCarVO getBy2OrderId(Integer myOrderId) {
        Myorder myorder = orderMapper.selectByPrimaryKey(myOrderId);
        Car car = carMapper.selectByPrimaryKey(myorder.getCarId());
        OrderCarVO vo = new OrderCarVO(myorder, car);
        return vo;
    }
}
